<?php

namespace App\Service\Impl;

use App\Service\Mobile;

if (!defined('IN_PX'))
    exit;

use Phoenix\Log\Log4p as logger;

class MobileImpl implements Mobile {

    const VERSION = '1.1.2';

    private function __Service() {}

    private function __Inject($cache) {}

//    private $_apikey = '42d4aa4132b45969c49789b566e13440';
    private $_apikey = '42ff3fd78386d7dd9ad38a968ee041b1';
    /*http://www.yunpian.com/product.html
    用户名：15968306399
    密码：wsm0688*/

    /**
     * 智能匹配模版接口发短信
     * apikey 为云片分配的apikey
     * text 为短信内容
     * mobile 为接受短信的手机号
     */

    
    public function sock_post($mobile, $code, $type, $userName = '', $porductName = '', $name = '', $money = '', $status = 0) {
        $apikey = "42ff3fd78386d7dd9ad38a968ee041b1"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
        $text = '';
//        $mobile = "13681929794"; //请用自己的手机号代替
        switch (intval($type)) {
            case 0:
                $text="【昂立一课】您的验证码是" . $code;
                break;
            case 1:
                $text = "【昂立一课】" . $userName . "您好，感谢您购买了" . $porductName . "~  您可以在“个人中心”找到已购买的课程或资料~免费咨询电话4008208062~";
                break;
            case 2:
                $text = "【昂立一课】老师您好，您的验证码为" . $code . "，如非本人操作请忽略此短信。";
                break;
            case 3:
                $text = "【昂立一课】" . $name . "您好，恭喜您获得了" . $money . "元优惠券，您可以在个人中心—我的优惠券中查看~免费咨询电话4008208062~";
                break;
            case 4:
                $text = "【昂立一课】亲爱的" . $userName . "，课程名" . $porductName . "，课程时间：" . $name . "，马上就要上课了，请登入个人中心—我的直播课程，进教室吧！";
                break;
            case 5:
                $text = "【昂立一课】亲爱的手机尾号为" . $userName . "的学员，您上传的问题已经有老师解答啦~快登录昂立一课网站—个人中心—我的课程中查看吧~";
                break;
            case 6:
                $text = "【昂立一课】亲爱的昂立一课会员，您申请的退款已经审核通过，退款金额为：" . $userName . "元,在3~5个工作日内将原路退回到您的付款账户，请注意查看。";
                break;
        }

        // 发送短信
        $data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);

        $ch = curl_init();
        /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));
        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        if ($status = 0) {
            $json_data = $this->single_send($ch,$data);    // 单发
        } else {
            $json_data = $this->batch_send($ch,$data);       // 群发
        }
        $array = json_decode($json_data,true);
        curl_close($ch);
        return $array;
    }

    private function single_send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }

    private function batch_send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/batch_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }
}
